<template>
  <div class="register">
    <div class="registerHeader">
      <Header class="homeHeader" />
    </div>
    <div class="registerMain">
      <h3 class="title">{{ $t("register") }}</h3>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="email">
          <el-input
            v-model="ruleForm.email"
            :placeholder="$t('email')"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item prop="code">
          <div class="flex items-center flex-1 justify-start">
            <el-input
              v-model="ruleForm.code"
              :placeholder="$t('codePlaceholder')"
              size="large"
            >
            </el-input>
  
            <el-button
              size="large"
              type="primary"
              class="ml-4"
              @click="handleCaptcha"
              :disabled="ruleForm.email === ''"
              v-if="!codesms.disabled"
            >
              {{ $t("getCode") }}
            </el-button>
            <el-button size="large" type="primary" class="ml-4" disabled v-else>
              {{ codesms.count }}{{ $t("codeTips") }}
            </el-button>
          </div>
        </el-form-item> -->
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            :placeholder="$t('password')"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="newPassword">
          <el-input
            v-model="ruleForm.newPassword"
            :placeholder="$t('confirmPassword')"
            type="password"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="btn" @click="register('ruleForm')">
        {{ $t("toRegister") }}
      </div>
      <div class="tips">
        {{ $t("loginTips") }}
        <a @click="isShow = true">{{ $t("toLogin") }}</a>
      </div>
    </div>
    <div class="footer">
      <Footer />
    </div>
    <LoginDialog
      v-if="isShow"
      @close="
        () => {
          isShow = false;
        }
      "
    />
  </div>
</template>
<script>
import { httpEmailSend, httpMobileRegister, httpInfo } from "@/http/api";
import Header from "./header.vue";
import Footer from "./footer.vue";
import LoginDialog from "@/components/loginDialog.vue";
export default {
  name: "register",
  components: { Header, Footer, LoginDialog },
  data() {
    return {
      ruleForm: {
        email: "",
      },
      loginForm: {},
      rules: {
        email: [
          {
            required: true,
            message: this.$t("emailTips"),
            trigger: "change",
          },
        ],
        password: [
          {
            required: true,
            message: this.$t("passwordTips"),
            trigger: "change",
          },
          {
            min: 8,
            max: 16,
            message: this.$t("passwordTips"),
            trigger: "change",
          },
        ],
        newPassword: [
          {
            required: true,
            message: this.$t("passwordAgain"),
            trigger: "change",
          },
          {
            min: 8,
            max: 16,
            message: this.$t("passwordTips"),
            trigger: "change",
          },
        ],
        // code: [
        //   {
        //     required: true,
        //     message: this.$t("codePlaceholder"),
        //     trigger: "blur",
        //   },
        // ],
      },
      codesms: {
        disabled: false,
        total: 60,
        count: 0,
      },
      timer: null,
      isShow: false,
    };
  },
  methods: {
    // 倒计时
    timerHandle() {
      this.codesms.count = this.codesms.total;
      this.codesms.disabled = true;

      this.timer = setInterval(() => {
        if (
          this.codesms.count > 1 &&
          this.codesms.count <= this.codesms.total
        ) {
          this.codesms.count--;
        } else {
          this.codesms.disabled = false;
          clearInterval(this.timer);
        }
      }, 1000);
    },

    //发送验证码
    async handleCaptcha() {
      try {
        this.timerHandle();
        let params = { email: this.ruleForm.email, code_type: "register" };
        let res = await httpEmailSend(params);
        if (res.code === 0) {
          this.$message({
            type: "success",
            message: "发送验证码成功",
          });
        }
      } catch (error) {
        clearInterval(this.timer);
      }
    },
    register(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let params = {
            account_name: this.ruleForm.email,
            password: this.ruleForm.password,
            again_password: this.ruleForm.newPassword,
            code: this.ruleForm.code,
            invite_code: this.ruleForm.invitationCode || "",
            type: "email",
          };
          let res = await httpMobileRegister(params);
          if (res.code == 0) {
            localStorage.setItem("TOKEN", res.data.token);
            let res2 = await httpInfo();
            if (res2.code == 0) {
              localStorage.setItem("email", this.ruleForm.email);
              localStorage.setItem("account_name", res2.data.account_name);
              localStorage.setItem("invite_code", res2.data.invite_code);
              localStorage.setItem("uid", res2.data.uid);
              this.$emit("loginSuccess");
            }
          } else {
            this.$message({
              type: "error",
              message: res.message,
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  mounted() {},
};
</script>
<style lang="less" scoped>
.register {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  .registerHeader {
    width: 100%;
    .homeHeader {
      width: 100%;
      height: 50px;
    }
  }
  .footer {
    width: 100%;
  }
  .registerMain {
    width: 1200px;
    padding-bottom: 150px;
    padding-top: 50px;
    min-height: 668px;
    .title {
      font-size: 30px;
      color: #333;
    }
    .demo-ruleForm {
      margin-top: 20px;
      .el-input {
        width: 60%;
        margin-right: 10px;
      }
      span {
        color: #b8b8b8;
        font-size: 16px;
      }
    }
    .btn {
      width: 60%;
      text-align: center;
      height: 50px;
      line-height: 50px;
      background: #4a2d12;
      color: #fff;
      border-radius: 20px;
      font-size: 20px;
      cursor: pointer;
    }
    .tips {
      width: 60%;
      margin-top: 20px;
      text-align: center;
      font-size: 16px;
      color: #999;
      a {
        color: #624931;
        cursor: pointer;
      }
    }
  }
}
</style>
